#include "editmenu.h"
#include "ui_editmenu.h"

#include <QMouseEvent>
#include <QPixmap>


EditMenu::EditMenu(QList<DictData> sys_show_hide,QString menuName,qlonglong menuId,
                   bool flag,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditMenu)
{
    ui->setupUi(this);
    //setWindowFlags(Qt::FramelessWindowHint); //不显示边框
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    setWindowTitle("编辑菜单");
    this->sys_show_hide = sys_show_hide;
    tree = new QTreeWidget(this);
    iconDialog = new IconDialog(this);
    iconDialog->hide();
//    QPixmap search(":/image/res/what.jpg");
    QPixmap what(":/image/res/what.jpg");
    what = what.scaled(15,15);
    ui->label_menuIcon->setPixmap(what);
    ui->label_outLink->setPixmap(what);
    ui->label_componentPath->setPixmap(what);
    ui->label_routerParam->setPixmap(what);
    ui->label_showStatus->setPixmap(what);
    ui->label_routerLocation->setPixmap(what);
    ui->label_authorityChar->setPixmap(what);
    ui->label_cache->setPixmap(what);
    ui->label_menuStatus->setPixmap(what);
    if(!flag){
        setWindowTitle("编辑菜单");
    }

    menu.menuType = 'M';
    if(!flag){
        menu.menuId = menuId;
    }else{
        menu.parentId = menuId;
        menu.menuId = 0;
    }
    this->flag = flag;
    init();

    connect(iconDialog,&IconDialog::checkIcon,this,[=](const QString &iconName){
        menu.icon = iconName;
        QPixmap p(":/svg/res/svg/" + iconName + ".svg");
        ui->label_menuIcon->setPixmap(p.scaled(20,20));
        ui->lineEdit_menuIconName->setText(iconName);
    });
    connect(tree,&QTreeWidget::itemClicked,this,[=](QTreeWidgetItem *item){
        menu.parentId = item->data(1,0).toInt();
        ui->lineEdit_parentManu->setText(item->text(0).mid(0,item->text(0).indexOf("(")));
        tree->hide();
    });

    ui->lineEdit_parentManu->setText(menuName);
    ui->lineEdit_parentManu->installEventFilter(this);
    ui->lineEdit_menuIconName->installEventFilter(this);
    ui->radioButton_directory->setChecked(1);
    tree->installEventFilter(this);
    ui->label_parentMenu->installEventFilter(this);
    iconDialog->installEventFilter(this);
}

EditMenu::~EditMenu()
{
    delete ui;
}

bool EditMenu::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress){
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::LeftButton){
            if(obj == ui->lineEdit_parentManu){
                if(tree->isHidden()){
                    ui->lineEdit_parentManu->clear();
                    tree->setFixedSize(ui->widget_14->width(),250);
                    tree->show();
                    tree->move(ui->widget_14->geometry().bottomLeft());
                }else{
                    tree->hide();
                }
            }
            if(obj == ui->lineEdit_menuIconName){
                if(iconDialog->isHidden()){
                    iconDialog->setFixedSize(450,300);
                    iconDialog->move(ui->widget_13->geometry().bottomLeft());
                    iconDialog->show();
                }else{
                    iconDialog->hide();
                }
            }
            if(obj == ui->label_parentMenu){
                ui->lineEdit_parentManu->clear();
            }
            return true;
        }else{
            return false;
        }
    }else if(event->type() == QEvent::Enter){

        return true;
    }else if(event->type() == QEvent::Leave){
        if(obj == tree){
            tree->close();
        }
        if(obj == iconDialog){
            iconDialog->close();
        }
        return true;
    }else{
        return QObject::eventFilter(obj, event);
    }
}

bool EditMenu::event(QEvent *event)
{
    if (event->type() == QEvent::ActivationChange){
//        if(QApplication::activeWindow() != tree){
//            qDebug()<<3;
//            tree->close();
//        }
//        if(QApplication::activeWindow() != userManagerHideColumnDlg){
//            userManagerHideColumnDlg->close();
//        }
    }
    return QWidget::event(event);
}

void EditMenu::init()
{
    ui->widget_9->hide();
    ui->widget_10->hide();
    ui->widget_11->hide();
    ui->widget_12->hide();
    this->setFixedHeight(410);
    ui->radioButton_show1->setText(sys_show_hide.at(0).dictLabel);
    ui->radioButton_show0->setText(sys_show_hide.at(1).dictLabel);

    getList();
    initTree();

    if(!flag){
        QByteArray *data = menuManagerApi.getById(menu.menuId);
        QJsonObject obj = QJsonDocument::fromJson(*data).object();
        if(obj.value("code").toInt() != 200){
            return;
        }

        menu = Menu::toMenu(obj.value("data").toObject());
        QString type = menu.menuType;
        if(type == 'M'){
            ui->radioButton_directory->setChecked(1);
        }else if(type == 'C'){
            ui->radioButton_menu->setChecked(1);
        }else{
            ui->radioButton_btn->setChecked(1);
        }
        if(menu.icon != "#"){
            ui->label_menuIcon->setPixmap(QPixmap(":/svg/res/svg/" + menu.icon + ".svg").scaled(20,20));
        }
        ui->lineEdit_menuIconName->setText(menu.icon);
        ui->lineEdit_menuName->setText(menu.menuName);
        ui->spinBox_menuSort->setValue(menu.orderNum);
        menu.isFrame == "0" ? ui->radioButton_outLink1->setChecked(1) : ui->radioButton_outLink0->setChecked(1);
        ui->lineEdit_routerLocation->setText(menu.path);
        ui->lineEdit_componentPath->setText(menu.component);
        ui->lineEdit_authorityChar->setText(menu.perms);
        ui->lineEdit_routerParam->setText(menu.query);
        menu.isCache == "0" ? ui->radioButton_cache1->setChecked(1) : ui->radioButton_cache0->setChecked(1);
        menu.visible == "0" ? ui->radioButton_show1->setChecked(1) : ui->radioButton_show0->setChecked(1);
        menu.status == "0" ? ui->radioButton_show1->setChecked(1) : ui->radioButton_show1->setChecked(1);

    }
}

void EditMenu::getList()
{
    menuList.clear();
    QByteArray *data = menuManagerApi.list("","");
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() != 200){
        return;
    }
    QJsonArray arr = obj.value("data").toArray();
    for(int i = 0;i< arr.size();i++){
        menuList.append(Menu::toMenu(arr.at(i).toObject()));
    }
}

void EditMenu::initTree()
{
    tree->hide();
    tree->setHeaderHidden(1);
    tree->clear();
    QTreeWidgetItem *mainMenu = new QTreeWidgetItem(tree);
    mainMenu->setText(0,"主类目");
    mainMenu->setData(1,0,0);
    for(int i = 0;i< menuList.size();i++){
        if(menuList.at(i).parentId == 0){
            QTreeWidgetItem *item = new QTreeWidgetItem(mainMenu);
            item->setText(0,menuList.at(i).menuName);
            item->setData(1,0,menuList.at(i).menuId);
            setChildTree(item);
        }
    }
    QTreeWidgetItem *item = tree->topLevelItem(0);
    setTreeChildCount(item);
}

void EditMenu::setChildTree(QTreeWidgetItem *item)
{
    for(int i = 0;i< menuList.size();i++){
        if(menuList.at(i).parentId == item->data(1,0).value<qulonglong>()){
            QTreeWidgetItem *child = new QTreeWidgetItem(item);
            child->setText(0,menuList.at(i).menuName);
            child->setData(1,0,menuList.at(i).menuId);
            setChildTree(child);
        }
    }
}

void EditMenu::setTreeChildCount(QTreeWidgetItem *item)
{
    QString text = ui->lineEdit_parentManu->text();
    int count = item->childCount();
    if(count == 0){
        return;
    }
    for(int i = 0;i< item->childCount();i++){
        QTreeWidgetItem *child = item->child(i);
        if(!text.isEmpty()){
            if(!child->text(0).contains(text,Qt::CaseInsensitive)){
                count--;
            }
        }
        setTreeChildCount(child);
    }
    item->setText(0,item->text(0).mid(0,item->text(0).indexOf("(")) + "(" + QString::number(count) + ")");
}

bool EditMenu::inputCheck()
{
    if(menu.menuType == 'M' || menu.menuType  == 'C'){
        if(ui->lineEdit_menuName->text().isEmpty() || ui->lineEdit_routerLocation->text().isEmpty()){
            ui->lineEdit_menuName->setStyleSheet("border: 1px solid red;border-radius:10px;");
            ui->lineEdit_routerLocation->setStyleSheet("border: 1px solid red;border-radius:10px;");
            return 0;
        }else{
            ui->lineEdit_menuName->setStyleSheet("border-radius:10px;");
            ui->lineEdit_routerLocation->setStyleSheet("border-radius:10px;");
            return 1;
        }
    }else{
        if(ui->lineEdit_menuName->text().isEmpty()){
            ui->lineEdit_menuName->setStyleSheet("border: 1px solid red;border-radius:10px;");
            return 0;
        }else{
            ui->lineEdit_menuName->setStyleSheet("border-radius:10px;");
            return 1;
        }
    }
}

void EditMenu::on_btn_cancel_clicked()
{
    close();
}

void EditMenu::on_btn_ok_clicked()
{
    if(!inputCheck()){
        return;
    }
    menu.component = ui->lineEdit_componentPath->text();
    menu.icon = ui->lineEdit_menuIconName->text();
    menu.isCache = ui->radioButton_cache1->isChecked() ? "0" : "1";
    menu.isFrame = ui->radioButton_outLink1->isChecked() ? "0" : "1";
    menu.menuName = ui->lineEdit_menuName->text();
    menu.orderNum = ui->spinBox_menuSort->text().toInt();
    menu.path = ui->lineEdit_routerLocation->text();
    menu.perms = ui->lineEdit_authorityChar->text();
    menu.query = ui->lineEdit_routerParam->text();
    menu.status = ui->radioButton_status1->isChecked() ? "0" : "1";
    menu.visible = ui->radioButton_show1->isChecked() ? "0" : "1";
    QByteArray *data = menuManagerApi.menu(menu);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    QMessageBox::warning(0,"提示",obj.value("msg").toString());

    if(obj.value("code").toInt() == 200){
        emit success();
        close();
    }
}

void EditMenu::on_lineEdit_parentManu_textChanged(const QString &arg1)
{
    int items = tree->topLevelItemCount();
    for(int i = 0;i< items;i++){
        QTreeWidgetItem *treeWidget = tree->topLevelItem(i);
        utils.filterTreeWidget(treeWidget,arg1);
    }
    arg1 == "" ? tree->collapseAll() : tree->expandAll();
    QTreeWidgetItem *item = tree->topLevelItem(0);
    setTreeChildCount(item);
}


void EditMenu::on_radioButton_directory_clicked()
{
    menu.menuType = 'M';
    this->setFixedHeight(410);
    ui->widget_9->hide();
    ui->widget_10->hide();
    ui->widget_11->hide();
    ui->widget_12->hide();
    ui->label_4->show();
    ui->label_router1->show();
    ui->widget_4->show();
    ui->widget_5->show();
    ui->widget_6->show();
    ui->widget_7->show();
    ui->widget_13->show();
}


void EditMenu::on_radioButton_menu_clicked()
{
    menu.menuType = 'C';
    this->setFixedHeight(523);
    ui->label_4->show();
    ui->label_router1->show();
    ui->widget_13->show();
    ui->widget_4->show();
    ui->widget_5->show();
    ui->widget_6->show();
    ui->widget_7->show();
    ui->widget_9->show();
    ui->widget_10->show();
    ui->widget_11->show();
    ui->widget_12->show();
}


void EditMenu::on_radioButton_btn_clicked()
{
    menu.menuType = 'F';
    this->setFixedHeight(323);
    ui->label_4->hide();
    ui->label_router1->hide();
    ui->widget_4->hide();
    ui->widget_6->hide();
    ui->widget_5->hide();
    ui->widget_7->hide();
    ui->widget_9->hide();
    ui->widget_10->show();
    ui->widget_11->hide();
    ui->widget_12->hide();
    ui->widget_13->hide();
}

